package cn.shop.front.config.mybatis_plus;

import cn.shop.front.util.CurrentUserUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author jzw
 * @since 2021-11-02 12:06
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		log.info("start insert fill ....");
		strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class);
		strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
		Long loginId = CurrentUserUtil.getUserId();
		strictInsertFill(metaObject, "createId", Long.class, loginId);
		strictUpdateFill(metaObject, "updateId", Long.class, loginId);
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		log.info("start update fill ....");
		strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
		Long loginId = CurrentUserUtil.getUserId();
		strictUpdateFill(metaObject, "updateId", Long.class, loginId);
	}

}